package com.lemon.ai.socket.packets.spiritgame.model.generator.utils;

import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringUtils.firstToUpperCase;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringUtils.generateClassName;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringUtils.generateParameterName;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringUtils.generatePropertyFromColumn;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringUtils.hump;

import java.util.ArrayList;
import java.util.List;

import com.lemon.ai.socket.packets.spiritgame.model.db.MetaData;
import com.lemon.ai.socket.packets.spiritgame.model.dto.Column;
import com.lemon.ai.socket.packets.spiritgame.model.dto.Property;
import com.lemon.ai.socket.packets.spiritgame.model.dto.Table;

public class DbUtils {

	private static String PS = "ps.setPROPERTY_TYPE(COUNT, PROPERTY_NAME);";
	private static String RS = "CLASS_PARAMETER.setPARAMETER_NAME(rs.getPROPERTY_TYPE(\"PROPERTY_NAME\"));";

	/**
	 * 把字段的整型数据类型更新为属性的字符串型数据类型
	 * 
	 * @param type
	 *            字段数据类型
	 * @return 属性数据类型
	 */
	public static String updateTypeFromDb(int type) {
		String t = "";
		switch (type) {
		case -5:
			t = "long";
			break;
		case -1:
			t = "String";
			break;
		case 1:
			t = "char";
			break;
		case 4:
			t = "int";
			break;
		case 6:
			t = "float";
			break;
		case 8:
			t = "double";
			break;
		case 12:
			t = "String";
			break;
		case 16:
			t = "boolean";
			break;
		case 91:
			t = "Date";
			break;
		case 93:
			t = "Date";
			break;
		}
		return t;
	}

	/**
	 * 根据该表的所有属性生成ADD_METHOD里面的COLUMN_NAMES字符串
	 * 
	 * @param cs
	 *            字段List
	 * @return COLUMN_NAMES字符串
	 */
	public static String generateColumnNames(List<Column> cs) {
		String s = cs.get(0).getName();
		for (int i = 1; i < cs.size(); i++)
			s += ", " + cs.get(i).getName();
		return s;
	}

	/**
	 * 根据表的属性数量生成ADD_METHOD里面的COLUMN_?S字符串
	 * 
	 * @param cs
	 *            字段List
	 * @return COLUMN_?S字符串
	 */
	public static String generateColumn_s(List<Column> cs) {
		String s = "?";
		for (int i = 1; i < cs.size(); i++)
			s += ", ?";
		return s;
	}

	/**
	 * add方法   insert into *** 
	 * 
	 * 				.....
	 * 
	 * 省略号部分即返回值
	 * @param table
	 * @return
	 */
	public static CharSequence generatePsSets_add(Table table) {
		List<Column> cs = table.getColumns();
		
//		for (Column column : cs) {
//			System.out.println(column);
//		}
		
		String parameterName = generateParameterName(table.getName());
		cs = new ArrayList<Column>(cs);
		String ps = "";
		for (int i = 0; i < cs.size(); i++) {
			Property p = generatePropertyFromColumn(cs.get(i));
			
			ps += PS;
			String prop = parameterName + ".get"
					+ generateClassName(p.getName()) + "()";
			if ("Date".equals(p.getType()))
				prop = "new java.sql.Date(" + prop + ".getTime())";
		
			ps = ps.replace("PROPERTY_TYPE", generateClassName(p.getType())).
					replace( "COUNT", "" + (i + 1)).replace("PROPERTY_NAME", prop);
			//保证格式的工整
			if(i < cs.size() - 1)
				ps += "\r\t\t\t";
		}
		
//		System.out.println(ps);
		return ps;
	}
	
	/**
	 * 根据表列的集合和类名生成PS_SETS字符串，如ps.setInt(1, user.getId());
	 */
	public static String generatePsSets_update(Table table) {
		List<Column> cs = table.getColumns();
		String parameterName = generateParameterName(table.getName());
		
		
		cs = new ArrayList<Column>(cs);
		
		
		Column key = table.getKey();		//将key提到最后面
		boolean su = cs.remove(key);
		if(su) {
			cs.add(key);
		}
		
		
		String ps = "";
		for (int i = 0; i < cs.size(); i++) {
			Property p = generatePropertyFromColumn(cs.get(i));
			ps += PS;
			String prop = parameterName + ".get"
					+ generateClassName(p.getName()) + "()";
			if ("Date".equals(p.getType())) {
				prop = "new java.sql.Date(" + prop + ".getTime())";
			}
			ps = ps.replace( "PROPERTY_TYPE", generateClassName(p.getType())).
					replace( "COUNT", "" + (i + 1)).
					replace("PROPERTY_NAME", prop);
			//保证格式的工整
			if(i < cs.size() - 1)
				ps += "\r\t\t\t";
		}
		
		return ps;
	}

	/**
	 * 生成设置分页参数的PsSets
	 * 
	 * @return PAGE_SETS字符串
	 */
	public static String generatePageSets() {
		String ps = "";
		ps += "ps.setInt(1, (pageNo - 1) * pageSize);\r\t\t\t";
		ps += "ps.setInt(2, pageSize);";
		return ps;
	}

	/**
	 * 根据Table对象得到该对象的delete方法的ps.set部分，如ps.setInt(1, id);
	 * 
	 * @param table
	 *            表格对象
	 * @return delete方法的ps.set部分
	 */
	public static String generatePkPsSets(Table table) {
		Column pk = table.getKey();
		String p = "ps.set" + 
				generateClassName(updateTypeFromDb(pk.getType())) + 
				"(" + 1 + ", " + pk.getName()+ ");\r\t\t";
		return p;
	}

	/**
	 * 根据Table对象得到该表格的主键参数，如：int sno, int cno
	 * 
	 * @param table
	 *            表格对象
	 * @return 主键参数
	 */
	public static String generatePkParameters(Table table) {
		Column pk = table.getKey();
		String p = updateTypeFromDb(pk.getType()) + " " + pk.getName();
		return p;
	}

	/**
	 * 根据Table对象得到该对象的sql语句的主键部分，如：sno = ? AND cno = ?
	 * 
	 * @param table
	 *            表格对象
	 * @return sql语句的主键部分
	 */
	public static String generatePkColumns(Table table) {
		Column pk = table.getKey();
		String p = pk.getName() + " = ?";
		return p;
	}

	/**
	 * 根据Column List获得该对象的update sql语句的where子句部分
	 * 
	 * @return
	 */
	public static String generateColumnParameters(Table table) {
		
		ArrayList<Column> columns = new ArrayList<Column>(table.getColumns());
		
		columns.remove(table.getKey());
		
		String p = "";
		for (int i = 0; i < columns.size(); i++) {
			Column c = columns.get(i);
			if (c.getType() == 12 || c.getType() == -1)
				p += c.getName() + " = ?";
			else
				p += c.getName() + " = ?";
			if (i != columns.size() - 1)
				p += ", ";
		}
		return p;
	}

	public static String generateRsGets(Table table) {
		List<Column> columns = table.getColumns();
		String classParameter = generateParameterName(table.getName());
		String rs = "";
		for (int i = 0; i < columns.size(); i++) {
			Column c = columns.get(i);
			rs += RS;
			rs = rs.replace("CLASS_PARAMETER", classParameter).
					replace("PARAMETER_NAME", firstToUpperCase(hump(c.getName()))).								//成员变量名
					replace("PROPERTY_TYPE", generateClassName(updateTypeFromDb(c.getType()))).	//类型名, String , int long.....
					replace("PROPERTY_NAME", c.getName());
			
			System.out.println(firstToUpperCase(hump(c.getName())));
			
			//保证格式的工整
			if(i < columns.size() - 1)
				rs += "\r\t\t";
		}
		return rs;
	}

	public static void main(String[] args) {
		MetaData meta = MetaData.getInstance();
		List<Column> labels = meta.getColumnsByTableName("user");
		for (int i = 0; i < labels.size(); i++) {
			Column label = labels.get(i);
			System.out.println(label.getTypename() + ": " + label.getType());
		}
	}

}
